//
//  CommonView.m
//  jang
//
//  Created by lee han on 03/04/2017.
//  Copyright © 2017 lee han. All rights reserved.
//

#import "CommonView.h"
#import "MBProgressHUD.h"
#import "MainController.h"

static UIStoryboard* sStoryboard;
static UIColor* sNavigationColor;

static UIAlertController *sMessageview;

@implementation CommonView

+ (void)addLoadingView{
    MBProgressHUD* loadingView = [[MBProgressHUD alloc] initWithWindow:[[UIApplication sharedApplication] keyWindow]];
    loadingView.labelText = @"加载中";
    loadingView.tag = 4003;
    [[[UIApplication sharedApplication] keyWindow] addSubview:loadingView];
    [loadingView show:YES];
}

+(void)removeLoadingView{
    MBProgressHUD* loadingView = (MBProgressHUD*)[[[UIApplication sharedApplication] keyWindow] viewWithTag:4003];
    if (loadingView)
    {
        [loadingView removeFromSuperview];
    }
}

+(void)addAlertView: (UIViewController*)owner Text: (NSString*) text{
    UIAlertController *alertview=[UIAlertController alertControllerWithTitle:text message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    // 设置按钮
    UIAlertAction *defult = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alertview addAction:defult];
    
    if(owner == nil)
        owner = [MainController sharedInstance];
    [owner presentViewController:alertview animated:YES completion:nil];
}

+(void)addMessageView: (NSString*) text{
    if(sMessageview == nil){
        sMessageview=[UIAlertController alertControllerWithTitle:text message:nil preferredStyle:UIAlertControllerStyleAlert];
    }
    else{
        [sMessageview setTitle:text];
    }
    
    [[MainController sharedInstance] presentViewController:sMessageview animated:YES completion:nil];
    
    [NSTimer scheduledTimerWithTimeInterval:1 repeats:NO block:^(NSTimer* time){
        [sMessageview dismissViewControllerAnimated:YES completion:nil];
    }];
}

//获取当前屏幕显示的viewcontroller
+ (UIViewController *)getCurrentVC
{
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}

+ (NSString *)toJSONData:(id)theData{
    
    NSError *error = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:theData
                        
                                                       options:NSJSONWritingPrettyPrinted
                        
                                                         error:&error];
    
    if ([jsonData length] > 0 && error == nil){
        
        return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }else{
        
        return nil;
        
    }
    
}

+ (NSDictionary *)toNSDictionary:(NSData *)jsonData{
    
    NSError *error = nil;
    
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                     
                                                    options:NSJSONReadingAllowFragments
                     
                                                      error:&error];
    
    if (jsonObject != nil && error == nil && [jsonObject isKindOfClass:[NSDictionary class]]) {
        
        return (NSDictionary*)jsonObject;
        
    } else {
        
        // 解析错误
        
        return nil;
        
    }
    
}

+(void) gotoViewController:(UIViewController*) se Target: (NSString*) storyboardid{
    if(sStoryboard == nil){
        sStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    }
    
    UIViewController *tVC = [sStoryboard instantiateViewControllerWithIdentifier:storyboardid];
    [se presentViewController:tVC animated:YES completion:nil];
}

+(void)setNativationBar:(UIViewController *)controller {
    [controller.navigationController.navigationBar setBarTintColor:[self NavigationColor]];
    [controller.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [controller.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    controller.navigationItem.backBarButtonItem = barButtonItem;
}

+(UIColor*) NavigationColor{
    if(sNavigationColor == nil)
        sNavigationColor = [UIColor colorWithRed:240/255.0f green:4/255.0f blue:30/255.0f alpha:1];
    return sNavigationColor;
}
@end
